package com.xujiang.mytool.model;

import android.text.TextUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.xujiang.common_core.util.tool.JsonUtils;
import com.xujiang.common_core.util.ourexception.SystemException;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xujiang
 * @version 2017-07-10 11:57
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class DirAndFileName {
    private String name;
    private String type;  //类型，是文件还是目录
    private String suffix;  //后缀,如果是目录 后缀设置为-1，如果有无后缀设置为nosuffix

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    @Override
    public String toString() {
        return "DirAndFileName{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", suffix='" + suffix + '\'' +
                '}';
    }

    /**
     * 将json数据转换成包含这个数据类的列表
     * @param json
     * @return
     * @throws SystemException
     */
    public static List<DirAndFileName> JsonDirAndFileNameAdapter(String json) throws SystemException{
        if(TextUtils.isEmpty(json)){
            throw new SystemException(SystemException.DATA_ERROR,"json is null");
        }
        try{
            JSONArray files = new JSONArray(json);
            if(files.length() <=0){
                return null;
            }
            List<DirAndFileName> dirAndFileNameList = new ArrayList<>();
            for(int i=0;i<files.length();i++){
                String s = files.getJSONObject(i).toString();
                DirAndFileName dirAndFileName = JsonUtils.parse(s,DirAndFileName.class);
                dirAndFileNameList.add(dirAndFileName);
            }
            return dirAndFileNameList;
        }catch (JSONException e){
            throw new SystemException(SystemException.DATA_ERROR,e);
        }
    }
}
